//
//  BGStatusOriginalView.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/24.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit
import SnapKit

class BGStatusOriginalView: UIView {
    // 记录当前原创微博的底部约束
    var orginalViewBottomConstraint:Constraint?
    
    var cellModel: BGHomeCellModel?{
        didSet{
            // 设置头像
            headImageView.bg_setImageWithURL(cellModel?.statusModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            // 昵称
            nameLabel.text = cellModel?.statusModel?.user?.name
            
            // 时间
            timeLabel.text = cellModel?.createdTime

            //来源
            sourceLabel.attributedText = cellModel?.sourceAttr
            
            // 微博会员等级
            memberImageView.image = cellModel?.memberImage
            // 微博认证用户
            avatarImageView.image = cellModel?.avatarImage
            
            contentLabel.text = cellModel?.statusModel?.text
            
            //为图片视图赋值
            //卸载约束
            orginalViewBottomConstraint?.uninstall()
            
            if cellModel?.statusModel?.pic_urls?.count>0 {
                //
                pictureView.picsModel = cellModel?.statusModel?.pic_urls
                //更新约束
                self.snp_makeConstraints { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(ViewMargin_S).constraint
                }
                pictureView.hidden = false
            }else {
                //更新约束
                self.snp_makeConstraints { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(ViewMargin_S).constraint
                }
                pictureView.hidden = true
            }
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI() {
        
        self.backgroundColor = WhiteColor
        
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
    }
    
    private func setupLayout() {
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(ViewMargin_S)
            make.size.equalTo(headImageSize)
        }
        // 02
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(ViewMargin_S)
        }
        // 03
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(ViewMargin_S)
            make.centerY.equalTo(nameLabel)
        }
        // 04
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 05
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(ViewMargin_S)
            make.bottom.equalTo(timeLabel)
        }
        // 06
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        // 07
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(ViewMargin_S)
            make.leading.equalTo(headImageView)
        }
        
        //08
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(ViewMargin_S)
            make.leading.equalTo(contentLabel).offset(ViewMargin_S)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        // 给当前原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
            self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(ViewMargin_S).constraint
        }
    }
    
    //MARK: - 懒加载
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imageName: "avatar_default")
    //用户名
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFontSize: 15)
    // 微博等级
    private lazy var memberImageView: UIImageView = UIImageView(imageName: "common_icon_membership")
    // 微博时间
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFontSize: 10)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: ThemeColor, textFontSize: 10)
    // 认证logo
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "avatar_vgirl")
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFontSize: 15,maxWidth: SCREEN_W - 20)
    //配图
    private lazy var pictureView: BGOriginalPicView = BGOriginalPicView()
    
}
